//
//  SwipeInitResultsViewController.m
//  MagTek.iDynamo.ObjC
//
//  Created by agharris73 on 9/19/13.
//  Copyright (c) 2013 Mercury. All rights reserved.
//

#import "SwipeInitResultsViewController.h"
#import "SwipeReturnViewController.h"

@interface SwipeInitResultsViewController ()

@end

@implementation SwipeInitResultsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.maskedTrack2DataResult.text = self.paymentInfo.track2Masked;
    self.encryptedTrack2DataResult.text = self.paymentInfo.track2Encrypted;
    self.ksnResult.text = self.paymentInfo.ksn;
   // NSLog(@"%@",self.paymentInfo.purchase);
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"processPayment"]) {
        self.paymentInfo.cvv = _securityCode.text;
        //SwipeVerifyViewController *target = (SwipeVerifyViewController *)segue.destinationViewController;
        //target.paymentInfo = self.paymentInfo;
    }
    
}


- (IBAction)nextstepButtonClick:(id)sender {
    
    NSMutableDictionary *dictionaryReq = [NSMutableDictionary new];
    
    [dictionaryReq setObject:self.paymentInfo.merchantId forKey:@"MerchantID"];
    [dictionaryReq setObject:self.paymentInfo.tranType forKey:@"TranType"];
    [dictionaryReq setObject:self.paymentInfo.tranCode forKey:@"TranCode"];
    [dictionaryReq setObject:self.paymentInfo.invoiceNo forKey:@"InvoiceNo"];
    [dictionaryReq setObject:self.paymentInfo.refNo forKey:@"RefNo"];
    [dictionaryReq setObject:self.paymentInfo.memo forKey:@"Memo"];
    [dictionaryReq setObject:self.paymentInfo.partialAuth forKey:@"PartialAuth"];
    [dictionaryReq setObject:self.paymentInfo.encryptedFormat forKey:@"EncryptedFormat"];
    [dictionaryReq setObject:self.paymentInfo.accountSource forKey:@"AccountSource"];
    [dictionaryReq setObject:self.paymentInfo.encryptedBlock forKey:@"EncryptedBlock"];
    [dictionaryReq setObject:self.paymentInfo.encryptedKey forKey:@"EncryptedKey"];
    [dictionaryReq setObject:self.paymentInfo.frequency forKey:@"Frequency"];
    [dictionaryReq setObject:self.paymentInfo.recordNo forKey:@"RecordNo"];
    [dictionaryReq setObject:self.paymentInfo.purchase forKey:@"Purchase"];
    [dictionaryReq setObject:self.paymentInfo.name forKey:@"Name"];
    [dictionaryReq setObject:self.paymentInfo.terminalName forKey:@"TerminalName"];
    [dictionaryReq setObject:self.paymentInfo.shiftID forKey:@"ShiftID"];
    [dictionaryReq setObject:self.paymentInfo.operatorID forKey:@"OperatorID"];
    [dictionaryReq setObject:self.paymentInfo.address forKey:@"Address"];
    [dictionaryReq setObject:self.paymentInfo.zip forKey:@"Zip"];
    //    [dictionaryReq setObject:self.paymentInfo.cvv forKey:@"CVV"];
   
    MercuryHelper *mgh = [MercuryHelper new];
    mgh.delegate = self;
    [mgh transctionFromDictionary:dictionaryReq andPassword:self.paymentInfo.password url:self.paymentInfo.primaryUrl tranInfo:@""] ;
    
}
-(void) transactionDidFailWithError:(NSError *)error {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"error" message:error.description delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
}

-(void) transactionDidFinish:(NSDictionary *)result {
    if (![[result objectForKey:@"CmdStatus"] isEqualToString:@"Approved"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[result objectForKey:@"CmdStatus"] message:[result objectForKey:@"TextResponse"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
       // [self cancelPayment:nil];
        return;
    }
     AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    SwipeReturnViewController *swipeReturnViewController=[[SwipeReturnViewController alloc]initWithNibName:@"SwipeReturnViewController" bundle:nil];
    swipeReturnViewController.resultDcit=result;
    paymentResultDTO *paymentRes = [paymentResultDTO new];
    if ([result objectForKey:@"CmdStatus"])
    {
        paymentRes.status = [result objectForKey:@"CmdStatus"];
    }
    if ([result objectForKey:@"TranCode"])
    {
        paymentRes.tranType = [result objectForKey:@"TranCode"];
    }
    if ([result objectForKey:@"AuthCode"])
    {
        paymentRes.authCode = [result objectForKey:@"AuthCode"];
    }
    if ([result objectForKey:@"Authorize"])
    {
        paymentRes.authAmount = [result objectForKey:@"Authorize"];
    }
    if ([result objectForKey:@"Purchase"])
    {
        paymentRes.amount = [result objectForKey:@"Purchase"];
    }
    if ([result objectForKey:@"TaxAmount"])
    {
        paymentRes.taxAmt = [result objectForKey:@"TaxAmount"];
    }
    if ([result objectForKey:@"AcqRefData"])
    {
        paymentRes.acqRefData = [result objectForKey:@"AcqRefData"];
    }
    if ([result objectForKey:@"CardType"])
    {
        paymentRes.cardType = [result objectForKey:@"CardType"];
    }
    if ([result objectForKey:@"TextResponse"])
    {
        paymentRes.displayMessage = [result objectForKey:@"TextResponse"];
    }
    if ([result objectForKey:@"ExpDate"])
    {
        paymentRes.expDate = [result objectForKey:@"ExpDate"];
    }
    if ([result objectForKey:@"InvoiceNo"])
    {
        paymentRes.invoice = [result objectForKey:@"InvoiceNo"];
    }
    if ([result objectForKey:@"AcctNo"])
    {
        paymentRes.maskedAccount = [result objectForKey:@"AcctNo"];
    }
    if ([result objectForKey:@"RefNo"])
    {
        paymentRes.refNo = [result objectForKey:@"RefNo"];
    }
    if ([result objectForKey:@"DSIXReturnCode"])
    {
        paymentRes.responseCode = [result objectForKey:@"DSIXReturnCode"];
    }
    if ([result objectForKey:@"CaptureStatus"])
    {
        paymentRes.statusMessage = [result objectForKey:@"CaptureStatus"];
    }
    if ([result objectForKey:@"RecordNo"])
    {
        paymentRes.token = [result objectForKey:@"RecordNo"];
    }
    if ([result objectForKey:@"CVVResult"])
    {
        paymentRes.cvvResult = [result objectForKey:@"CVVResult"];
    }
    if ([result objectForKey:@"ProcessData"])
    {
        paymentRes.processData = [result objectForKey:@"ProcessData"];
    }
      appDelegate.resultInf = paymentRes;
    
    /*NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"amount.txt"];
    
    [[result objectForKey:@"Purchase"] writeToFile:filePath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL];
    
    NSArray *paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory2 = [paths2 objectAtIndex:0];
    NSString *filePath2 = [documentsDirectory2 stringByAppendingPathComponent:@"authamount.txt"];
    
    [[result objectForKey:@"Authorize"] writeToFile:filePath2 atomically:TRUE encoding:NSUTF8StringEncoding error:NULL];*/
    swipeReturnViewController.resultInf=paymentRes;
    swipeReturnViewController.paymentInfo=self.paymentInfo;
    [self.navigationController pushViewController:swipeReturnViewController animated:NO];
  
   // NSLog(@"%@",result);
    
}
- (IBAction)cancelTransaction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    [self.paymentInfo cleanSwipeData];
}
@end
